package com.easylinkin.linkappapi.positioning.controller;

import com.easylinkin.linkappapi.positioning.entity.ElectronicFence;
import com.easylinkin.linkappapi.positioning.service.ElectronicFenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import java.util.List;

/**
 * @program: linkapp-group
 * @description: 电子围栏
 * @author: chenkaixuan
 * @create: 2021-07-27 11:38
 */
@RestController
@RequestMapping("/position/fence")
public class ElectronicFenceController {
    @Autowired
    ElectronicFenceService electronicFenceService;

    /**
     * 保存
     * @param electronicFence
     * @return
     */
    @PostMapping("saveElectronicFence")
    public RestMessage saveElectronicFence(@RequestBody ElectronicFence electronicFence){
        electronicFenceService.saveElectronicFence(electronicFence);
        return RestBuilders.successBuilder().build();
    }

    /***
     * 设置电子围栏
     * @param electronicFenceList
     * @return
     * @throws Exception
     */
    @PostMapping("configElectronicFence")
    public RestMessage configElectronicFence(@RequestBody ElectronicFence electronicFenceList) throws Exception {
        return electronicFenceService.configElectronicFence(electronicFenceList);
    }

    /***
     * 查询电子围栏
     * @param electronicFence
     * @return
     * @throws Exception
     */
    @PostMapping("electronicFenceList")
    public RestMessage electronicFenceList(@RequestBody ElectronicFence electronicFence) throws Exception {
        return RestBuilders.successBuilder().data(electronicFenceService.electronicFenceList(electronicFence)).build();
    }

    /***
     * 删除
     * @param electronicFence
     * @return
     * @throws Exception
     */
    @PostMapping("deleteElectronicFence")
    public RestMessage deleteElectronicFence(@RequestBody ElectronicFence electronicFence) throws Exception {
        return electronicFenceService.deleteElectronicFence(electronicFence);
    }

    /***
     * 保存告警配置
     * @param electronicFence
     * @return
     */
    @PostMapping("saveAlarmConfig")
    public RestMessage saveAlarmConfig(@RequestBody ElectronicFence electronicFence){
        electronicFenceService.saveAlarmConfig(electronicFence);
        return RestBuilders.successBuilder().build();
    }

    @PostMapping("getAlarmConfig")
    public RestMessage getAlarmConfig(@RequestBody ElectronicFence electronicFence){
        Assert.notNull(electronicFence,"参数不能为空");
        if(electronicFence.getId() != null){
            return RestBuilders.successBuilder().data(electronicFenceService.getAlarmConfigById(electronicFence)).build();
        }else {
            return RestBuilders.successBuilder().data(electronicFenceService.getAlarmConfigByIds(electronicFence)).build();
        }
    }
}
